package com.cnoc.base.config.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.cnoc.base.config.dao.ThreadControllerDao;
import com.cnoc.base.config.dto.ThreadControllerDto;
import com.cnoc.base.spring.base.BaseRepository;

@Repository
public class ThreadControllerDaoImpl extends BaseRepository<ThreadControllerDto> 
implements ThreadControllerDao {

	public List<ThreadControllerDto> qryThreadControllerDtos() {
		// TODO Auto-generated method stub
		String sql = "SELECT t.ID, t. NAME , t.QRY_THREAD_COUNT, t.DEAL_THREAD_COUNT,"
				+ " t.QRY_CLASS, t.DEAL_BEAN_ID, t.QRY_PARAMS, t.STATE, t.CREATE_DATE"
				+ " FROM IOM_OS_THREAD_CONTROLLER AS t" + " WHERE STATE='10A' ";
		List<ThreadControllerDto> threadControllerDtos = jdbcTemplate.query(
				sql, beanRowMap);

		return threadControllerDtos;
	}

	public List<ThreadControllerDto> qryThreadControllerDtos(String appCodeString) {
		// TODO Auto-generated method stub
		List<ThreadControllerDto> threadControllerDtos = null;
		List<Object> params = new ArrayList<Object>();
		StringBuilder sql = new StringBuilder(
				"SELECT t.ID, t. NAME , t.QRY_THREAD_COUNT, t.DEAL_THREAD_COUNT,"
						+ " t.QRY_CLASS, t.DEAL_BEAN_ID, t.QRY_PARAMS, t.STATE, t.CREATE_DATE"
						+ " FROM IOM_OS_THREAD_CONTROLLER AS t JOIN IOM_OS_APP_CONTROLLER  A "
						+ " ON A.THREAD_CONTROLLER_ID=T.ID AND A.STATE='1' "
						+ " WHERE t.STATE='10A' AND A.APP_CODE IN (");
		String[] appCodes = appCodeString.split(",");
		if (appCodes.length > 0) {
			for (int i = 0; i < appCodes.length; i++) {
				if (i == appCodes.length - 1) {
					sql.append("?)");
				} else {
					sql.append("?,");
				}
				params.add(appCodes[i]);
			}
			threadControllerDtos= jdbcTemplate.query(sql.toString(), params.toArray(), beanRowMap);
		}
		return threadControllerDtos;
	}
 

}
